package com.xiaoKe.zhonghzu.global.base;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.global.dialog.ToastDialog;
import com.xiaoKe.zhonghzu.global.tool.HideIMEUtil;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.global.view.ViewTitle;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;

/**
 * @Author : Shijingna
 * @Time : 2024/7/15 16:53
 * @Description : 基类
 */
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
    public T binding;

    public ToastDialog toastDialog = new ToastDialog();
    public List<String> permissionList = SPUtils.getList("permissionList");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Type superclass = getClass().getGenericSuperclass();
        Class<T> aClass = (Class<T>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            binding = (T) method.invoke(null, getLayoutInflater());
            setContentView(binding.getRoot());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        HideIMEUtil.wrap(Objects.requireNonNull(this));//键盘管理，点击除editText外区域收起键盘
        initTitle();
        initView();
        initData();


    }


    ViewTitle viewTitle;

    protected void initTitle() {
        if (null != findViewById(R.id.viewTitle)) {
            viewTitle = findViewById(R.id.viewTitle);
            viewTitle.initTitle(getIntent().getStringExtra("titleName"), getIntent().getStringExtra("titleNexName"), this);
        }
    }


    protected abstract void initView();

    protected abstract void initData();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        EventBus.getDefault().unregister(this);
    }

    protected void toFragmentPage(RouterConfig routerConfig) {
        AppRouter.toFragment(this, routerConfig);
    }

    protected void toFragmentForResult(RouterConfig routerConfig, int requestCode) {
        AppRouter.toFragmentForResult(this, routerConfig, requestCode);
    }

}
